<template>
  <div class="mod-log">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="请输入用户名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="searchForm.time"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :unlink-panels="true"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="exportData()">批量导出</el-button>
      </el-form-item>
    </el-form>
    
    <el-tables 
      :selecionType='selecionType'
      :table-data="dataList"
      :table-label="tableLabel"
      :table-option="tableOption"
      @handleSelectionChange="handleSelectionChange"
      @handleButton="handleButton"
      >
      <template slot="status" slot-scope="scope">
        <el-tag v-if="scope.row.status==='0'">启用</el-tag>
        <el-tag v-if="scope.row.status==='1'" type="danger">删除</el-tag>
        <el-tag v-if="scope.row.status==='2'" type="success">停用</el-tag>
        <el-tag v-if="scope.row.status==='3'" type="warning">冻结</el-tag>
      </template>
    </el-tables>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[13, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <log-detail ref="logDetail" />
  </div>
</template>

<script>
  import { formatTimeToStr } from '@/utils'
  import Vue from 'vue'
  export default {
    data () {
      return {
        tableLabel: [
          { label: '日志接收时间',
            param: 'logTime',
            minWidth: '30%',
            align: 'center',
            sortable: false,
            render: (row) => {
              return formatTimeToStr(row.logTime, 'yyyy-MM-dd hh:mm:ss')
            }
          },
          { label: '源IP', param: 'srcIp', minWidth: '25%', align: 'left', isShow: true },
          { label: '用户名称', param: 'userName', minWidth: '15%', align: 'left', isShow: true },
          { label: '日志类型', param: 'logType', minWidth: '20%', align: 'left', isShow: true },
          { label: '日志内容', param: 'content', align: 'left', isShow: true }
        ],
        tableOption: {
          label: '操作',
          width: '150',
          options: [
            { label: '查看日志内容', type: 'primary', methods: 'show' }
          ]
        },
        selecionType: 'index',
        searchForm: {
          name: '',
          time: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 13,
        totalPage: 0,
        selectionDataList: []
      }
    },
    created () {
      this.getDataList()
    },
    components: {
      'ElTables': () => import('@/components/el-table'),
      'LogDetail': () => import('../sys/log-detail')
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: '/operate/log/list',
          method: 'get',
          params: {
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.searchForm.name,
            'sTime': this.searchForm.time ? formatTimeToStr(this.searchForm.time[0], 'yyyy-MM-dd hh:mm:ss') : '',
            'eTime': this.searchForm.time ? formatTimeToStr(this.searchForm.time[1], 'yyyy-MM-dd hh:mm:ss') : ''
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      handleSelectionChange (val) {
      },
      handleButton (methods, row, index) {
        if (methods.methods === 'show') {
          this.$refs.logDetail.init(methods.row, true, 'oper')
        }
      },
      exportData () {
        window.location.href = `${window.SITE_CONFIG['baseUrl']}/operate/log/export?token=${Vue.cookie.get('token')}&sTime=${this.searchForm.time[0]}&eTime=${this.searchForm.time[1]}`
      }
    }
  }
</script>
